package com.qdlc.p2p.biz.service.additional.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.enums.EnumRuleType;
import com.qdlc.p2p.biz.service.additional.CardRuleService;
import com.qdlc.p2p.common.enums.EnumCardType;
import com.qdlc.p2p.common.util.CollectionUtils;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.dao.CardRuleDao;
import com.qdlc.p2p.dal.dto.CardRule;
import com.qdlc.p2p.dal.dto.RedEnvelope;
import com.qdlc.p2p.dal.model.CardRuleModel;
import com.qdlc.p2p.dal.model.RedEnvelopeModel;

/**
 * 卡片规则信息Service实现类
 *
 * @author Kaiju Zhao
 * @version V1.0.0
 * @date 2016-05-06
 */
@Service("cardRuleService")
public class CardRuleServiceImpl implements CardRuleService {

    @Resource
    private CardRuleDao cardRuleDao;

    @Override
    public CardRule saveCardRule(CardRuleModel model) {
        CardRule cardRule = CardRuleModel.prototype(model);
        cardRuleDao.save(cardRule);
        if (null != cardRule
                && 0L != cardRule.getId()) {
            return cardRule;
        }
        return null;
    }

    @Override
    public void deleteCardRule(long id) {
        cardRuleDao.delete(id);
    }

    @Override
    public int updateCardRule(CardRule cardRule) {
        return cardRuleDao.update(cardRule);
    }

    @Override
    public int updateCardRuleStatus(int status, int preStatus, long id) {
        return cardRuleDao.update(status, preStatus, id);
    }

    @Override
    public CardRuleModel findCardRuleById(long id) {
        CardRuleModel model = null;
        CardRule cardRule = cardRuleDao.findById(id);
        if (null != cardRule) {
            model = CardRuleModel.instance(cardRule);
        }
        return model;
    }

    @Override
    public List<CardRuleModel> findUsableCardRules() {
        List<CardRuleModel> modelList = null;
        List<CardRule> cardRuleList = cardRuleDao.findUsableCardRules();
        if (CollectionUtils.isNotNullOrEmpty(cardRuleList)) {
            modelList = new ArrayList<CardRuleModel>();
            for (CardRule cardRule : cardRuleList) {
                CardRuleModel cardRuleModel = CardRuleModel.instance(cardRule);
                cardRuleModel.setTypeStr(EnumCardType.getName(cardRule.getType()));
                modelList.add(cardRuleModel);
            }
        }
        return modelList;
    }

    @Override
    public PageDataList<CardRuleModel> list(CardRuleModel model) {
        PageDataList<CardRuleModel> pageDataList = new PageDataList<CardRuleModel>();
        PageDataList<CardRule> dataList = cardRuleDao.list(model);
        List<CardRule> cardRuleList = dataList.getList();
        List<CardRuleModel> modelList = new ArrayList<CardRuleModel>();
        if (CollectionUtils.isNotNullOrEmpty(cardRuleList)) {
            for (CardRule cardRule : cardRuleList) {
                CardRuleModel cardRuleModel = CardRuleModel.instance(cardRule);
                cardRuleModel.setTypeStr(EnumCardType.getName(cardRule.getType()));
                modelList.add(cardRuleModel);
            }
        }
        pageDataList.setPage(dataList.getPage());
        pageDataList.setList(modelList);
        return pageDataList;
    }

    @Override
    public int existCardRule(long id, int type, int status) {
        return cardRuleDao.existCardRule(id, type, status);
    }

}
